package com.rainng.coursesystem.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rainng.coursesystem.dao.mapper.EventMapper;
import com.rainng.coursesystem.model.bo.EventItemBO;
import com.rainng.coursesystem.model.bo.MemberEventSelectItemBO;
import com.rainng.coursesystem.model.entity.EventEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EventDAO extends BaseDAO {
    public static final int PAGE_SIZE = 20;

    private final EventMapper mapper;

    public EventDAO(EventMapper mapper) {
        this.mapper = mapper;
    }

    public int insert(EventEntity entity) {
        return mapper.insert(entity);
    }

    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    public EventEntity get(Integer id) {
        return mapper.selectById(id);
    }

    public int update(EventEntity entity) {
        return mapper.updateById(entity);
    }

    public int count(String areaName, String leaderName, String name) {
        return mapper.count(areaName, leaderName, name);
    }

    public List<EventItemBO> getPage(Integer index, String areaName, String leaderName, String name) {
        Page<EventItemBO> page = new Page<>(index, PAGE_SIZE);

        return mapper.getPage(page, areaName, leaderName, name).getRecords();
    }

    public Integer countByLeaderId(Integer leaderId) {
        LambdaQueryWrapper<EventEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EventEntity::getLeaderId, leaderId);

        return mapper.selectCount(wrapper);
    }

    public List<EventEntity> listName() {
        LambdaQueryWrapper<EventEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(EventEntity::getId, EventEntity::getName);

        return mapper.selectList(wrapper);
    }

    public int increaseSelectedCount(Integer eventId) {
        EventEntity event = mapper.selectById(eventId);
        event.setSelectedCount(event.getSelectedCount() + 1);

        return mapper.updateById(event);
    }

    public int decreaseSelectedCount(Integer eventId) {
        EventEntity event = mapper.selectById(eventId);
        event.setSelectedCount(event.getSelectedCount() - 1);

        return mapper.updateById(event);
    }

    public Integer countMemberCanSelect(Integer memberId, Integer areaId, Integer number, String eventName, String leaderName) {
        return mapper.countMemberCanSelect(memberId, areaId, number, eventName, leaderName);
    }

    public List<MemberEventSelectItemBO> getMemberCanSelectPage(Integer index, Integer memberId, Integer areaId, Integer number, String eventName, String leaderName) {
        Page<MemberEventSelectItemBO> page = new Page<>(index, PAGE_SIZE);

        return mapper.getMemberCanSelectPage(page, memberId, areaId, number, eventName, leaderName).getRecords();
    }

    public Integer getAreaIdById(Integer eventId) {
        return mapper.getAreaIdById(eventId);
    }
}
